/***********************************************************************
 * Dao Interface :  UrlResourceDao.java
 * Author:  Bill Vison
 * Purpose: Defines the Hibernate Implement UrlResourceDaoImpl
 ***********************************************************************/
package org.vsg.security.model.persistence.hibernate;

import java.io.Serializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import org.springframework.dao.DataAccessException;
import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.PaginationException;
import org.vsg.common.pagination.dao.PaginationHibernateDaoSupport;
import org.vsg.common.pagination.datatrans.PaginationProvider;
import org.vsg.security.model.domain.UrlResource;
import org.vsg.security.model.persistence.hibernate.proc.UrlResourceByAllPaginationProc;
import org.vsg.security.model.persistence.hibernate.proc.UrlResourcePaginationProc;
import org.vsg.security.model.persistence.UrlResourceDao;

/**
 * @author Bill Vison
 */
public class UrlResourceDaoImpl extends PaginationHibernateDaoSupport implements UrlResourceDao {

	@Override
	public Collection<String> findContextByAll() throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuffer query = new StringBuffer("select ");
		query.append("distinct resource.context ");
		query.append("from ").append(UrlResource.class.getName()).append(" as resource ");
		query.append("order by resource.context asc ");
		
		Iterator<String> iter = this.getHibernateTemplate().iterate(query.toString());
		
		// set the result values
		Collection<String> results = new Vector<String>();
		while (iter.hasNext()) {
			String context = iter.next();	
			
			results.add(context);
		}
		return results;
	}

	/**
	 * set the logger for develope code , do not use System.print(ln) to out print message.
	 */
	private static final Logger logger = LoggerFactory.getLogger(UrlResourceDaoImpl.class);

	
	/**
	 *
	 * @param urlResource
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.UrlResourceDao#save(org.vsg.security.model.domain.UrlResource)
	 */
	@Override
	public void save(UrlResource urlResource) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(urlResource);
	}

	@Override
	public void saveAll(Collection<UrlResource> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		
		getHibernateTemplate().saveOrUpdateAll(all);
		
	}   	
   	
	/**
	 *
	 * @param urlResource
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.UrlResourceDao#update(org.vsg.security.model.domain.UrlResource)
	 */
   	@Override
   	public void update(UrlResource urlResource) throws DataAccessException {
   		// TODO Auto-generated method stub
		getHibernateTemplate().update(urlResource);
	}   	


	/**
	 *
	 * @param urlResource
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.UrlResourceDao#delete(org.vsg.security.model.domain.UrlResource)
	 */
	@Override
	public void delete(UrlResource urlResource) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(urlResource);
	}

	@Override
	public void deleteAll(Collection<UrlResource> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().deleteAll(all);
	}
   	
   	
	/**
	 *
	 * @param pk , the primary key
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.UrlResourceDao#load(java.io.Serializable)
	 */
	@Override
	public UrlResource load(Serializable pk) throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		UrlResource urlResource = (UrlResource) getHibernateTemplate().load(UrlResource.class, pk);
		
		return urlResource;	
		// End of user code	
	}
   
   
	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.UrlResourceDao#pageUrlResource(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageUrlResource(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		UrlResourcePaginationProc proc = new UrlResourcePaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
		// End of user code	
	}      	


	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.UrlResourceDao#pageUrlResourceByAll(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageUrlResourceByAll(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		UrlResourceByAllPaginationProc proc = new UrlResourceByAllPaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
		// End of user code		
	}


	/**
	 * @exception DataAccessException
	 */
	@Override
	public Collection<UrlResource> findByAll() throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("select ");

		query.append("urlResource ");
		query.append("from ").append(UrlResource.class.getName()).append(" as urlResource ");
		query.append("where urlResource.flagDeleted = 0 ");
		query.append("order by urlResource.name");
		
		Collection params = new Vector();
		
		logger.info("query : " + query.toString());

		Iterator<UrlResource> iter = getHibernateTemplate().iterate(query.toString());
		
		Collection<UrlResource> result = new Vector<UrlResource>();
		
		while (iter.hasNext()) {
			result.add( iter.next() );
		}
		
		return result;
		// End of user code			
	}

	@Override
	public Collection<UrlResource> findUrlResourceByUrl(String url,
			String context) throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuffer query = new StringBuffer("select ");
		query.append("urlResource ");
		query.append("from ").append(UrlResource.class.getName()).append(" as urlResource ");
	
		StringBuffer queryCondition = new StringBuffer();
		
		boolean isFirstQuery = false;			

		Collection params = new Vector();
		
		if (url != null && !url.equals("")) {
			queryCondition.append("urlResource.url like ? ");
			params.add("%" + url + "%");
			isFirstQuery = true;
		}		
		
		if (context != null && !context.equals("")) {
			if (!isFirstQuery) {
				queryCondition.append("and ");
			} else {
				isFirstQuery = false;
			}			
			queryCondition.append("urlResource.context = ? ");
			params.add(context);
			isFirstQuery = true;
		}
		
		if (queryCondition.length() > 0  ) {
			query.append("where " + queryCondition);
		}		
		
		
		logger.info( query.toString() );

		
		Iterator<UrlResource> iter = this.getHibernateTemplate().iterate(query.toString() , params.toArray());
		
		// set the result values
		Collection<UrlResource> results = new Vector<UrlResource>();
		while (iter.hasNext()) {
			results.add( iter.next() );
		}
		return results;
	}

	@Override
	public Collection<UrlResource> findUrlResourcesByContext(String context)
			throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuffer query = new StringBuffer("select ");
		query.append("resource ");
		query.append("from ").append(UrlResource.class.getName()).append(" as resource ");
		query.append(" where resource.context = ? ");
		
		Collection params = new Vector();
		params.add(context);
		
		Iterator<UrlResource> iter = this.getHibernateTemplate().iterate(query.toString() , params.toArray());
		
		
		// set the result values
		Collection<UrlResource> results = new Vector<UrlResource>();
		while (iter.hasNext()) {
			UrlResource oriResource = iter.next();	
			
			results.add(oriResource);
		}
		return results;
	}

	@Override
	public Collection<UrlResource> findUrlResourceByCode(String code,
			String context) throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuffer query = new StringBuffer("select ");
		query.append("urlResource ");
		query.append("from ").append(UrlResource.class.getName()).append(" as urlResource ");
	
		StringBuffer queryCondition = new StringBuffer();
		
		boolean isFirstQuery = true;			

		Collection params = new Vector();
		
		if (code != null && !code.equals("")) {
			queryCondition.append("urlResource.code = ? ");
			params.add( code );
			isFirstQuery = false;
		}		
		
		if (context != null && !context.equals("")) {
			if (!isFirstQuery) {
				queryCondition.append("and ");
			} else {
				isFirstQuery = false;
			}			
			queryCondition.append("urlResource.context = ? ");
			params.add(context);
		}
		
		if (queryCondition.length() > 0  ) {
			query.append("where " + queryCondition);
		}		
		
		
		logger.info( query.toString() );

		
		Iterator<UrlResource> iter = this.getHibernateTemplate().iterate(query.toString() , params.toArray());
		
		// set the result values
		Collection<UrlResource> results = new Vector<UrlResource>();
		while (iter.hasNext()) {
			results.add( iter.next() );
		}
		return results;
	}

	
	
   
} 